﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using CuaHangLego.Bussiness;
using CuaHangLego.DataAccess;
using CuaHangLego.Model;

namespace CuaHangLego.UI
{
    public partial class QLKhoUI : UserControl
    {
        KhoBSS khobss;
        private int maxsize = 0;
        IList<ChuDe> listCD;
        IList<SanPham> listSP;

        public QLKhoUI()
        {
            InitializeComponent();
            tblKho.AutoGenerateColumns = false;
            tblNhatKy.AutoGenerateColumns = false;
            tblKho2.AutoGenerateColumns = false;
            txtNgay.Text = calDate.SelectionEnd.ToShortDateString();
            this.khobss = new KhoBSS();
            chart1.Series.Clear();
        }

        private void QLKhoUI_Load(object sender, EventArgs e)
        {
            if (!backgroundWorker1.IsBusy)
            {
                backgroundWorker1.RunWorkerAsync();
            }
        }

        private void calDate_DateChanged(object sender, DateRangeEventArgs e)
        {
            txtNgay.Text = calDate.SelectionEnd.ToShortDateString();
        }

        private void buttonXemTheoNgay_Click(object sender, EventArgs e)
        {
            tblKho.DataSource = khobss.GetNhatKyNgay(DateTime.Parse(txtNgay.Text));
        }

        private void buttonXem2_Click(object sender, EventArgs e)
        {
            if (tblNhatKy.SelectedRows.Count > 0)
            {
                IList<NhatKySanPham> l = khobss.GetNhatKySP((tblNhatKy.SelectedRows[0].DataBoundItem as SanPham).MaSo,
                    calNgayTu.Value, calNgayDen.Value);
                tblKho2.DataSource = l;
                VeDoThi(l);
            }
        }

        private void buttonCapNhat2_Click(object sender, EventArgs e)
        {
            khobss.UpdateData();
        }

        private void btKho_Loc_Click(object sender, EventArgs e)
        {
            tblKho.DataSource = khobss.GetNhatKyNgayBy(txtKeyword1.Text, cbxChuDe1.SelectedItem as ChuDe,
                string.IsNullOrEmpty(txtSLTu.Text) ? 0 : int.Parse(txtSLTu.Text),
                string.IsNullOrEmpty(txtSLDen.Text) ? int.MaxValue : int.Parse(txtSLDen.Text));
        }

        private void buttonXemTatCa1_Click(object sender, EventArgs e)
        {
            tblKho.DataSource = khobss.GetNhatKyNgayAll();
        }

        private void btKho_Tim_Click(object sender, EventArgs e)
        {
            tblNhatKy.DataSource = khobss.SearchSanPham(txtKeyword2.Text, cbxChuDe2.SelectedItem as ChuDe);
        }

        private void buttonTatCa2_Click(object sender, EventArgs e)
        {
            tblNhatKy.DataSource = khobss.GetAllSanPham();
        }

        public void VeDoThi(IList<NhatKySanPham> list)
        {
            if (list.Count > 0)
            {
                if (list.Count > maxsize) maxsize = list.Count;
                chart1.Width = 100 * maxsize;
                IList<int> yValues = (from n in list
                                 select n.SoLuong).ToList();
                IList<DateTime> xValues = (from n in list
                                           select n.ThoiGian).ToList();
                xValues.Insert(0, xValues.ElementAt(0));
                yValues.Add(yValues.ElementAt(yValues.Count - 1));
                try
                {
                    chart1.Series.Add(list[0].TenSanPham);
                }
                catch
                { }
                chart1.Series[list[0].TenSanPham].ChartType = System.Windows.Forms.DataVisualization.Charting.SeriesChartType.StepLine;
                chart1.Series[list[0].TenSanPham].BorderWidth = 3;
                chart1.Series[list[0].TenSanPham].Points.DataBindXY(xValues, yValues);
                chart1.ChartAreas["ChartArea1"].AxisX.LabelStyle.Interval = 1;
            }
        }

        private void buttonCapNhatNgay_Click(object sender, EventArgs e)
        {
            khobss.UpdateData();
        }

        private void buttonXoaDoThi_Click(object sender, EventArgs e)
        {
            chart1.Series.Clear();
            maxsize = 0;
        }

        private void backgroundWorker1_DoWork(object sender, DoWorkEventArgs e)
        {
            khobss.UpdateData();
            listSP = khobss.GetAllSanPham();
            listCD = ChuDeDA.Instance.GetAll();
            listCD.Insert(0, new ChuDe(-1, "Chọn chủ đề", ""));
        }

        private void backgroundWorker1_RunWorkerCompleted(object sender, RunWorkerCompletedEventArgs e)
        {
            tblNhatKy.DataSource = listSP;
            cbxChuDe1.DataSource = listCD;
            cbxChuDe2.DataSource = listCD;
        }

        private void buttonXuatBC_Click(object sender, EventArgs e)
        {
            khobss.Export(khobss.GetNhatKyNgay(DateTime.Parse(txtNgay.Text)));
        }
    }
}
